home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: Philippe Verdy <100105.3120@compuserve.com>
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: Linker error problem
- Date: 23 Mar 1996 23:18:39 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4j20sf$qnt@arl-news-svc-3.compuserve.com>
- NNTP-Posting-Host: ad53-232.compuserve.com
-
- boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles) s'Θcrit :
- > I am getting a linker error as follows. Undefined symbol
- > Address::ThisPtrList in module address.cpp.
- > I don't understand this non sense because it is defined
- > as a static class member and its value is initialized.
- > See $$$$ for definitions #### for causes of the error (if
- > I comment out these lines then the error disappears.
- >
- > // address.h
- > #include <iostream.h>
- >
- > #ifndef __ADDRESS_H
- > #define __ADDRESS_H
- > #define SIZE 100
- >
- > #include "defines.h"
- >
- > class Address
- > {
- > private : int Number;
- > char *Street;
- > char *City;
- > int Zip;
- > $$$$$$ static Address *ThisPtrList[SIZE];
- -------------------------------------------------------
- Here you declare: Address * Address::ThisPtrList[SIZE]
- -------------------------------------------------------
- > static unsigned int NumberInstances;
- > static unsigned int InstanceNumber;
- >
- > public : // Constructors
- > Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- > Address();
- > // Copy constructor
- > Address(Address& AnAddress);
- > // Deconstructor
- > ~Address();
- > // Operator overloads
- > friend ostream& operator << (ostream& OutPutStream,Address& AnAddress);
- > friend istream& operator >>(istream& InPutStream,Address& AnAddress);
- > // Functions
- > void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
- > unsigned int Address::NumberOfInstances();
- > void Address::DisplayAll();
- > };
- >
- > #endif
- >
- >
- >
- > // address.cpp
- >
- > #include "address.h"
- > #include <string.h>
- > #include <iostream.h>
- >
- > // Data initializations
- > unsigned int Address::NumberInstances=0;
- >
- > unsigned int Address::InstanceNumber=0;
- >
- > $$$$ Address Address::*ThisPtrList={0};
- ---------------------------------------------------
- The previous line is not correct. You only declare
- a pointer to a member of the class Address, which points
- to an object of type Address. This is valid even if there
- is no such member in the class Address !! (the compiler
- makes no assumption on WHICH member the pointer should
- point to, but only on the type of that member, so that
- dereferencing it thru an instance pointer could lead to
- a valid complete member reference.)
-
- You should write:
- Address * Address::ThisPtrList[SIZE] = {0};
- ---------------------------------------------------
- >
- > // Constructors
- > Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- > {
- > Number=ANumber;
- > Street=new char[strlen(AStreet)+1];
- > strcpy(Street,AStreet);
- > City=new char[strlen(ACity)+1];
- > strcpy(City,ACity);
- > Zip=AZip;
- > #### ThisPtrList[NumberInstances]=this;
- ---------------------------------------------------
- With the previous correction, this line is correct
- ---------------------------------------------------
- > NumberInstances++;
- > InstanceNumber=NumberInstances;
- > }
- >
- > Address::Address()
- > {
- > Number=0;
- > Street=new char[strlen("")+1];
- > strcpy(Street,"");
- > City=new char[strlen("")+1];
- > strcpy(City,"");
- > Zip=0;
- > #### ThisPtrList[NumberInstances]=this;
- ---------------------------------------------------
- With the previous correction, this line is correct
- ---------------------------------------------------
- > NumberInstances++;
- > InstanceNumber=NumberInstances;
- > }
- >
- > // Copy constructor
- > Address::Address(Address& AnAddress)
- > {
- > Number=AnAddress.Number;
- > Street=new char[strlen(AnAddress.Street)+1];
- > strcpy(Street,AnAddress.Street);
- > City=new char[strlen(AnAddress.City)+1];
- > strcpy(City,AnAddress.City);
- > Zip=AnAddress.Zip;
- > #### ThisPtrList[NumberInstances]=this;
- ---------------------------------------------------
- With the previous correction, this line is correct
- ---------------------------------------------------
- > NumberInstances++;
- > InstanceNumber=NumberInstances;
- > }
- >
- > // Deconstructor
- > Address::~Address()
- > {
- > Number=0;
- > delete Street;
- > delete City;
- > Zip=0;
- > }
- >
- > // Operator overloads
- > ostream& operator <<(ostream& OutPutStream,Address& AnAddress)
- > {
- > OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
- > OutPutStream<<"City : "<<AnAddress.City<<EOLN;
- > OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
- > return(OutPutStream);
- > }
- >
- >
- > istream& operator >>(istream& InPutStream,Address& AnAddress)
- > {
- > char *StreetName,*CityName,ch;
- >
- > StreetName=new char[50];
- > CityName=new char[50];
- > cout<<"Number : ";
- > cin>>AnAddress.Number;
- > GetEndOfLine();
- > cout<<"Street : ";
- > cin.getline(StreetName,49,EOLN);
- > delete AnAddress.Street;
- > AnAddress.Street=new char[strlen(StreetName)+1];
- > strcpy(AnAddress.Street,StreetName);
- > cout<<"City : ";
- > cin.getline(CityName,49,EOLN);
- > delete AnAddress.City;
- > AnAddress.City=new char[strlen(CityName)+1];
- > strcpy(AnAddress.City,CityName);
- > cout<<"City : ";
- > cout<<"Zip code : ";
- > }
-
-